﻿define(function(require,exports,module) {
    var myPub = require('./public');

    $(function() {
        var oFooter = $(".js_footer"); 					//底部
        var oGoodsStats = $('.js_goodsStats');          // 收货状态选择
        var oReasonList = $('.js_reasonList');          // 售后原因选择

        var oSubmitApply = $('.js_submitApply');        // 提交申请
        var oSubmitAppeal = $('.js_submitAppeal');        // 提交申诉申请
        // var oTelPhone = $('.js_telPhone');				//手机输入框


        var $goodsStatus = $('#goodsStatus');            // 收货状态
        var $applyCause = $('#applyCause');              // 申请原因
        var $expressNo = $('#expressNo');              // 提货码编码
        // var $codeKey = $('#codeKey');              // 提货码密码

        var oShadow = $('.js_shadow');                  //阴影加载图

        var oApplyReasons = $('.js_ApplyReasons');		//申请原因点击
        var oReasonsResult = $('.js_reasonsResult');	//申请原因选择的结果
        var oCloseBtn = $('.js_closeBtn');				//弹框关闭按钮
        var bSideOff = true;							//阻止多次点击开关
        var oCouponSeletSide = $('.js_couponSeletSide');	// 弹框
        var moveHeight = oCouponSeletSide.outerHeight(true);    //弹框高度
        var orefundButton = $("#refund");				//金额
        oCouponSeletSide.css({'bottom': -moveHeight });

        var isSubmitClicked = false;
        // 提交售后申请
        oSubmitApply.hammer().on('tap',function() {

            if(isSubmitClicked){
                console.log("重复点击")
                return;
            }
            if (validate()){
                //myPub.loadLayer();
                //$('.layermcont').append('<p style="padding-top:15px;">正在提交，请稍后...</P>');
                oShadow.show().find('.js_loadPng').addClass('myRotateRefresh');

                //按钮防止重复点击
                isSubmitClicked = true;
                $.ajax({
                    url: "/shop/workOrder/applyDeliveryCodeAfterSale",
                    type: "POST",
                    data: $("#apply-form").serialize(),
                    dataType:'json',
                    success: function(data) {
                        $('.layermcont').find('p').remove();    // 移除图片加载文字
                        myPub.LayerCloseAll();
                        if(data.status == 'success'){
                            myPub.TipLayerEnd(data.message,'',function(){
                                window.location.href = "/shop/pay/order_view?orderSn=" + data.orderSn;
                            },'');
                        }else{
                            isSubmitClicked = false;
                            myPub.TipLayer(data.message);
                        }
                    },
                    error: function(){
                        //$('.layermcont').find('p').remove();    // 移除图片加载文字
                        //myPub.LayerCloseAll();
                        oShadow.hide().find('.js_loadPng').removeClass('myRotateRefresh');
                    },
                    complete: function(XMLHttpRequest, textStatus){
                        XMLHttpRequest = null;
                        oShadow.hide().find('.js_loadPng').removeClass('myRotateRefresh');
                        oFooter.hide()
                    }
                });
            }else{
                console.info('validate failed');
            }
        });

        //提交申诉申请
        oSubmitAppeal.hammer().on('tap',function() {
            if(isSubmitClicked){
                return;
            }
            if (appealValidate()){
                //myPub.loadLayer();
                //$('.layermcont').append('<p style="padding-top:15px;">正在提交，请稍后...</P>');
                oShadow.show().find('.js_loadPng').addClass('myRotateRefresh');

                isSubmitClicked =true;
                $.ajax({
                    url: "/shop/workOrder/applyAppeal",
                    type: "POST",
                    data: $("#apply-form").serialize(),
                    dataType:'json',
                    success: function(data) {
                        $('.layermcont').find('p').remove();    // 移除图片加载文字
                        myPub.LayerCloseAll();
                        if(data.status == 'success'){
                            myPub.TipLayerEnd(data.message,'',function(){
                                window.location.href = "/shop/workOrder/showDetail/" + data.workOrderId +"?oId=" + data.oId;
                            },'');
                        }else{

                            isSubmitClicked =false;
                            myPub.TipLayer(data.message);
                        }
                    },
                    error: function(){
                        //$('.layermcont').find('p').remove();    // 移除图片加载文字
                        //myPub.LayerCloseAll();
                        oShadow.hide().find('.js_loadPng').removeClass('myRotateRefresh');
                    },
                    complete: function(XMLHttpRequest, textStatus){
                        XMLHttpRequest = null;
                        oShadow.hide().find('.js_loadPng').removeClass('myRotateRefresh');
                        oFooter.hide()
                    }
                });
            }else{
                console.info('validate failed');
            }
        });



        // 商品状态选择
        oGoodsStats.hammer().on('tap',function() {
            var _offTap = $(this).parents('.js_offTap').attr('offTap')

            if(_offTap == 'true') {
                var index = $(this).index()
                oReasonList.hide().eq(index).show();  //切换原因
                oReasonList.find('li').removeClass('active');
                oReasonsResult.text('请选择');
                $applyCause.val('');


                oGoodsStats.find('.js_checkBox').removeClass('active');
                $(this).find('.js_checkBox').addClass('active');


                $goodsStatus.val($(this).find('.active').data("status"));
            }


        });


        // 售后原因选择
        oReasonList.find('li').hammer().on('tap',function() {
            oReasonList.find('li').removeClass('active');
            $(this).addClass('active');
            oReasonsResult.text($(this).find('span').eq(0).text());
            $applyCause.val($(this).find('span').eq(0).text());

            oCouponSeletSide.stop().animate({bottom:-moveHeight},300,function(){
                oCouponSeletSide.hide();
                oShadow.hide();
                $('html').removeClass('bodyHeight100');
                bSideOff = true
            });
        });

        // 绑定焦点丢失事件
/*        orefundButton.on('blur',function(){
            checkRefund();
        });*/

        //手机验证
        // oTelPhone.find('input').on('input propertychange',function(){
        //     var _val = $(this).val();
        //     var oClearBtn = $('.js_clearBtn');
        //     if(_val == '') {
        //         oClearBtn.hide();
        //     }
        //     else {
        //         oClearBtn.show();
        //     }
        //
        //
        //     if( checkPhoneNumber(_val) ) {
        //         oTelPhone.removeClass('has_error').addClass('has_success')
        //     }
        //     else {
        //         oTelPhone.addClass('has_error').removeClass('has_success')
        //     }
        // });

        // 申请原因弹框显示和消失
        oApplyReasons.hammer().on('tap', function () {
            if(!bSideOff)  return;
            bSideOff = false;

            oCouponSeletSide.show().stop().animate({ 'bottom':0 },300,function() {
                oShadow.show();
                $('html').addClass('bodyHeight100');
                bSideOff = true
            });

        });

        oCloseBtn.hammer().on('tap', function () {
            //隐藏服务
            oCouponSeletSide.stop().animate({bottom:-moveHeight},300,function(){
                oCouponSeletSide.hide();
                oShadow.hide();
                $('html').removeClass('bodyHeight100');
                bSideOff = true
            });
            myPub.HamstopPropaga();
        });

        oShadow.hammer().on('tap', function () {
            //隐藏服务
            oCouponSeletSide.stop().animate({bottom:-moveHeight},300,function(){
                oCouponSeletSide.hide();
                oShadow.hide();
                $('html').removeClass('bodyHeight100');
                bSideOff = true
            });
            myPub.HamstopPropaga();
        });
    });

    function validate() {
        var pgs = $('#goodsStatus').val(),applyCause = $('#applyCause').val(),
            description = $("#description").val().trim(), expressNo=$('#expressNo').val().trim();
        if(!pgs){
            myPub.TipLayer("请选择收货状态");
            return false;
        }

        if(!applyCause) {
            myPub.TipLayer("请选择申请原因");
            return false;
        }

        // if (deliveryCodePay !=1){
            // if (!expressNo){
            //     myPub.TipLayer("请输入退货运单号");
            //     return false;
            // }
            // if (!checkExpressNo(expressNo,12)){
            //     myPub.TipLayer("运单号格式不正确");
            //     return false;
            // }
        // }
        if(!description) {
            myPub.TipLayer("请输入问题描述");
            return false;
        }

/*        if(!checkRefund()){
            return false;
        }*/

        return true;
    }

    //申诉参数校验
    function appealValidate() {
        var applyCause = $('#applyCause').val();
        var appealDesc = $('#appealDesc').val();
        if(!applyCause) {
            myPub.TipLayer("请选择平台介入原因");
            return false;
        }
        if(!appealDesc) {
            myPub.TipLayer("请输入问题描述");
            return false;
        }

        return true;
    }

    //校验电话号码
    function checkPhoneNumber(v){
        var regMobile = /^1[3|4|5|6|7|8|9]\d{9}$/;;   //验证手机号码
        var regPhone = /^(^0\d{2}-?\d{8}$)|(^0\d{3}-?\d{7}$)|(^\(0\d{2}\)-?\d{8}$)|(^\(0\d{3}\)-?\d{7}$)$/;   //验证固定电话
        if(regMobile.test(v) || regPhone.test(v)){
            return true;
        }
        return false;
    }

    function checkExpressNo(v,l) {
        if (v.length == l){
            return true;
        }
        return false;
    }

    function checkRefund() {
        var tmpv = $("#refund").val();
        if(!/^[0-9]+(.[0-9]{0,2})?$/.test(tmpv)){
            myPub.TipLayer("协商退款输入框请输入数字,支持两位有效小数");
            return false;
        }
        if( parseFloat(maxMoney)  < parseFloat(tmpv)  ){
            myPub.TipLayer("协商退款输入框输入金额需要小于"+maxMoney);
            return false;
        }
        if(parseFloat(tmpv)<= parseFloat(0)){
            myPub.TipLayer("协商退款输入框输入金额需要大于"+0);
            return false;
        }
        return true;
    }

});



